package register;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

import co.yeye.test.MainActivity;
import co.yeye.test.R;
import models.UserObject;

/**
 * Created by yeye on 15/2/21.
 */
public class User extends Activity {

    Button btn_register,btn_login,btn_logout,btn_modify,tittle;
    ImageView user_image;
    TextView text_username,text_info,text_name,text_code;
    ImageLoader imageLoader;
    DisplayImageOptions options;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100){
            if(resultCode==RESULT_OK){
                //setMessage();
                finish();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user);

        tittle = (Button) findViewById(R.id.tittle);
        btn_register = (Button) findViewById(R.id.btn_confirm);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_logout = (Button) findViewById(R.id.btn_logout);
        btn_modify = (Button) findViewById(R.id.btn_modify);
        user_image = (ImageView) findViewById(R.id.user_img);
        text_username = (TextView) findViewById(R.id.user_username);
        text_info = (TextView) findViewById(R.id.user_info);
        text_name = (TextView) findViewById(R.id.user_name);
        text_code = (TextView) findViewById(R.id.user_code);

        tittle.setVisibility(View.GONE);

        imageLoaderInit();
        setMessage();
    }

    private void setMessage(){
        UserObject userObject = new UserObject();
        userObject = MainActivity.getInstance().getUser();
        // 没有登录
        if(userObject.getUser_name().equals("-1")){
            btn_register.setVisibility(View.VISIBLE);
            btn_login.setVisibility(View.VISIBLE);
            btn_logout.setVisibility(View.GONE);
            btn_modify.setVisibility(View.GONE);
            text_info.setVisibility(View.GONE);
            text_name.setVisibility(View.GONE);
            text_code.setVisibility(View.GONE);
            text_username.setText("请登录");
            btn_register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(User.this,register.class);
                    startActivity(intent);
                    finish();
                }
            });
            btn_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(User.this,Login.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
        // 已登录
        else{
            btn_register.setVisibility(View.GONE);
            btn_login.setVisibility(View.GONE);
            btn_logout.setVisibility(View.VISIBLE);
            btn_modify.setVisibility(View.VISIBLE);
            text_info.setVisibility(View.VISIBLE);
            text_name.setVisibility(View.VISIBLE);
            text_code.setVisibility(View.VISIBLE);
            text_username.setText(userObject.getUser_name());
            text_name.setText(userObject.getName());
            text_code.setText(userObject.getUser_code());
            imageLoader.displayImage(userObject.getUri(),user_image,options,null);

            btn_logout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AVUser avUser = AVUser.getCurrentUser();
                    avUser.logOut();
                    MainActivity.getInstance().logOut();
                    user_image.setImageDrawable(getResources().getDrawable(R.drawable.user_blank_null));
                    setMessage();
                }
            });

            btn_modify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(User.this,UserModify.class);
                    startActivityForResult(intent,100);
                }
            });

        }
    }

    private void imageLoaderInit(){
        File cacheDir = StorageUtils.getOwnCacheDirectory(getBaseContext(), "imageloader/Cache");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(getBaseContext())
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
                .build();//开始构建
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);//全局初始化此配置

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_launcher)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)						// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)							// 设置下载的图片是否缓存在SD卡中
                        //.resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
                        //.displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
                        //.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                .build();//构建完成
    }

}
